/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.put.dcs_modeler.project.actions;

import java.awt.event.ActionEvent;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.openide.awt.DynamicMenuContent;
import org.openide.awt.Mnemonics;
import org.openide.util.ContextAwareAction;
import org.openide.util.Lookup;
import org.openide.util.actions.Presenter;
import pl.put.dcs_modeler.exceptions.actions.PublicationException;
import pl.put.dcs_modeler.project.actions.publicAction.PublicAction;
import pl.put.dcs_modeler.project.actions.publicAction.PublicActionProvider;
import pl.put.dcs_modeler.project.projectPropertiesExtensions.dcsProjectProperties.projectConfigModel.DCSServerConfig;
import pl.put.dcs_modeler.utils.DCSProjectUtil;

/**
 *
 * @author LukaszR
 */
public class PublicProjectAction extends AbstractAction implements ContextAwareAction
{

    public static Logger log = Logger.getLogger( PublicProjectAction.class.getName() );

    @Override
    public void actionPerformed( ActionEvent e )
    {
        assert false;
    }

    @Override
    public Action createContextAwareInstance( Lookup context )
    {
        return new ContextAction( context );
    }

    private boolean enable( Project p )
    {
        assert p != null;

        return DCSProjectUtil.isDCSProject( p );
    }

    private String labelFor( Project p )
    {
        assert p != null;
        return "&Public " + ProjectUtils.getInformation( p ).getDisplayName() + " on server";
    }

    private void perform( Project p )
    {
        assert p != null;
        // TODO what to do when run
        PublicActionProvider prov = new PublicActionProvider();
        List< DCSServerConfig > svr = DCSProjectUtil.getServersConfig( p );
        if ( svr.size() > 0 )
        {
            DCSServerConfig cfg = svr.get( 0 );
            PublicAction pa = prov.createPublicOnLocalServerAction( p, cfg.getServerPath() );
            try
            {
                pa.perform();
            }
            catch ( PublicationException ex )
            {
                log.warning( ex.getMessage() );
            }
        }
    }

    private final class ContextAction extends AbstractAction implements Presenter.Popup
    {

        private final Project p;

        public ContextAction( Lookup context )
        {
            Project _p = context.lookup( Project.class );
            p = ( _p != null && enable( _p ) ) ? _p : null;
        }

        public void actionPerformed( ActionEvent e )
        {
            perform( p );
        }

        public JMenuItem getPopupPresenter()
        {
            class Presenter extends JMenuItem implements DynamicMenuContent
            {

                public Presenter()
                {
                    super( ContextAction.this );
                }

                public JComponent[] getMenuPresenters()
                {
                    if ( p != null )
                    {
                        Mnemonics.setLocalizedText( this, labelFor( p ) );
                        return new JComponent[]
                                {
                                    this
                                };
                    }
                    else
                    {
                        return new JComponent[ 0 ];
                    }
                }

                public JComponent[] synchMenuPresenters( JComponent[] items )
                {
                    return getMenuPresenters();
                }
            }
            return new Presenter();
        }
    }
}
